package hn.cch.advanced.thread;

public class ThreadLocalDemo {

    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public void setThreadLocal() {
        threadLocal.set(Thread.currentThread().getName());
    }

    public String getThreadLocal() {
        return threadLocal.get();
    }

    public static void main(String[] args) throws InterruptedException {

        ThreadLocalDemo threadLocalDemo = new ThreadLocalDemo();
        threadLocalDemo.setThreadLocal();
        System.out.println("start:" + threadLocalDemo.getThreadLocal());

        Thread thread = new Thread(() -> {
            threadLocalDemo.setThreadLocal();
            System.out.println("thread:" + threadLocalDemo.getThreadLocal());
        }, "thread");

        thread.start();
        thread.join();

        System.out.println("stop:" + threadLocalDemo.getThreadLocal());
        // 解决内存泄漏问题
        threadLocalDemo.threadLocal.remove();
    }


}
